package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/users")
@RestController
@RequiredArgsConstructor
public class UserController {

    // 推荐构造函数注入
    private final IUserService userService;

    @ApiOperation("新增用户")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userFormDTO) {
        // 1. 把dto拷贝到po
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        // 2. 新增
        userService.save(user);
    }
    @ApiOperation("删除用户接口")
    @DeleteMapping("{id}")
    public void deleteUserById(@ApiParam("用户id") @PathVariable("id") Long id) {
        userService.removeById(id);
    }
    @ApiOperation("根据id查询用户接口")
    @GetMapping("{id}")
    public UserVO queryUserById(@ApiParam("用户id") @PathVariable("id") Long id) {
        // 查询用户po
        User user = userService.getById(id);
        System.out.println(user);
        // 把po拷贝到vo
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        System.out.println(userVO);
        return userVO;
    }

    @ApiOperation("根据id查询用户及地址接口")
    @GetMapping("andAddress/{id}")
    public UserVO queryUserWithAddressById(@ApiParam("用户id") @PathVariable("id") Long id) {
        return userService.queryUserAndAddressById(id);
    }

    @ApiOperation("根据id批量查询用户接口")
    @GetMapping
    public List<UserVO> queryUserByIds(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids) {
        // 查询用户po
//        List<User> users = userService.listByIds(ids);
//        // 把po拷贝到vo
//        List<UserVO> userVOS = BeanUtil.copyToList(users, UserVO.class);
//        return userVOS;
        return userService.queryUserAndAddressByIds(ids);
    }
    @ApiOperation("扣减用户余额接口")
    @PutMapping("{id}/deduction/{money}")
    public void deductMoneyUserById(
            @ApiParam("用户id") @PathVariable("id") Long id,
            @ApiParam("扣减金额") @PathVariable("money") Integer money) {
        userService.deductBalance(id, money);
    }

    @ApiOperation("根据复杂条件查询用户接口")
    @GetMapping("/list")
    public List<UserVO> queryUsers(UserQuery userQuery) {
        // 查询用户po
        List<User> users = userService.queryUsers(
                userQuery.getName(),userQuery.getStatus(),userQuery.getMinBalance(),userQuery.getMaxBalance());
        // 把po拷贝到vo
        List<UserVO> userVOS = BeanUtil.copyToList(users, UserVO.class);
        return userVOS;
    }

    @ApiOperation("根据条件分页查询用户接口")
    @GetMapping("/page")
    public PageDTO<UserVO> queryUsersPage(UserQuery query) {
        return userService.queryUserPage(query);
    }
}
